package chapter.two.six;

/**
 * 2.6 使用读写锁实现同步数据访问
 * 使用读操作锁时，可以允许多个线程访问，但是在使用写操作锁时只允许一个线程操作
 * 也就是一个线程在执行写操作时，其他线程不能够执行读操作
 *
 */
public class Main {

	/**
	 * Main class of the example
	 * 
	 * @param args
	 */
	public static void main(String[] args) {

		// Creates an object to store the prices
		PricesInfo pricesInfo = new PricesInfo(1.0, 2.0);

		Reader readers[] = new Reader[5];
		Thread threadsReader[] = new Thread[5];

		// Creates five readers and threads to run them
		for (int i = 0; i < 5; i++) {
			readers[i] = new Reader(pricesInfo);
			threadsReader[i] = new Thread(readers[i]);
		}

		// Creates a writer and a thread to run it
		Writer writer = new Writer(pricesInfo);
		Thread threadWriter = new Thread(writer);

		// Starts the threads
		for (int i = 0; i < 5; i++) {
			threadsReader[i].start();
		}
		threadWriter.start();

	}

}
